# -*- coding:utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.http import HttpResponseRedirect
from wenlincms.core.admin import OwnableAdmin
from wenlincms.utils.urls import admin_url

from wlmodels.codes.utils import EntIdableAdmin
from .models import *


# 客户消费记录
class ELFeeLogAdmin(admin.ModelAdmin):
    list_display = ["id", "customer", "item", "fee", "count", "ip", "created", ]
    list_display_links = ["customer", ]
    list_filter = ["customer", ]
    search_fields = ["customer__username", ]
    readonly_fields = ["customer", "item", "fee", "tkey", "count", "ip", "created", ]

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_actions(self, request):
        actions = super(ELFeeLogAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions


# 客户充值记录
class ELCustomerChargeLogAdmin(OwnableAdmin):
    fieldsets = ((None, {"fields": ["user", "customer", "money", "smoney", "beizhu", "created"]}),)
    list_display = ["id", "user", "customer", "money", "smoney", "beizhu", "created"]
    list_display_links = ["customer", ]
    raw_id_fields = ["customer", ]
    readonly_fields = ["user", "customer", "money", "smoney", "created", ]
    search_fields = ["customer__username", ]

    def save_form(self, request, form, change):
        return OwnableAdmin.save_form(self, request, form, change)

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_actions(self, request):
        actions = super(ELCustomerChargeLogAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions


# 为客户充值
class ELCustomerChargeAdmin(ELCustomerChargeLogAdmin):
    fieldsets = ((None, {"fields": ["customer", "money", "smoney", "beizhu", "created"]}),)
    readonly_fields = ["created", ]

    def has_add_permission(self, request, obj=None):
        return True

    def changelist_view(self, *args, **kwargs):
        return HttpResponseRedirect(admin_url(ELCustomerCharge, "add"))


# SAAS系统公告信息
class ELSaasMsgAdmin(OwnableAdmin):
    list_display = ["id", "user", "title", "content", "created", ]
    list_display_links = ["title", ]
    list_filter = ["user", ]
    search_fields = ["title", ]
    readonly_fields = ["user", "created", ]


# 生命周期参数表
class ELStatisticsLifeAdmin(admin.ModelAdmin):
    list_display = ["id", "INDUSTRYPHY", "DOMDISTRICT_id", "REGCAP", "life_k", "life_r", "method", ]
    ordering = ("INDUSTRYPHY",)


# 我的监控
class ELMonitorAdmin(EntIdableAdmin):
    list_display = ["id", "customer", "entid", "created", ]
    raw_id_fields = ["customer", ]
    readonly_fields = ["created", ]


# 风险预警消息
class ELRiskMsgAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "title", "content", "created", ]
    list_display_links = ["title", ]
    search_fields = ["title", ]
    readonly_fields = ["created", ]


#admin.site.register(ELFeeLog, ELFeeLogAdmin)
#admin.site.register(ELCustomerCharge, ELCustomerChargeAdmin)
#admin.site.register(ELCustomerChargeLog, ELCustomerChargeLogAdmin)
# admin.site.register(ELSaasMsg, ELSaasMsgAdmin)
# admin.site.register(ELStatisticsLife, ELStatisticsLifeAdmin)
# admin.site.register(ELMonitor, ELMonitorAdmin)
# admin.site.register(ELRiskMsg, ELRiskMsgAdmin)
